package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	@Override
	public List<JSONObject> getBaseCategoryList() {
		//new一个JSONObject对象
		List<JSONObject> list = new ArrayList<>();


		//调用封装好的视图来查询一级二级三级所有的记录集合
		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);

		//分组时用category1Id来分组,key为category1Id , value为指定分组后的所对应的List.
		Map<Long, List<BaseCategoryView>> category1IdList = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		//遍历分组后的集成的map来进行遍历
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = category1IdList.entrySet().iterator();
		 while(iterator.hasNext()){
			 Map.Entry<Long, List<BaseCategoryView>> basecategoryView = iterator.next();
			 //获取一类标签为1的list
			 List<BaseCategoryView> list1 = basecategoryView.getValue();
			 //创建一个JSONObject对象
			 JSONObject jsonObject = new JSONObject();
			 //因为id为1的所有名字都一样,所以取第一个的name就行
			 jsonObject.put("categoryName",list1.get(0).getCategory1Name());
			 jsonObject.put("categoryId",list1.get(0).getCategory1Id());
			 Map<Long, List<BaseCategoryView>> map2 = list1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			 Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator2 = map2.entrySet().iterator();
			List<JSONObject> child2 = new ArrayList<>();
			 while (iterator2.hasNext()){
				 Map.Entry<Long, List<BaseCategoryView>> next2 = iterator2.next();
				 List<BaseCategoryView> list2 = next2.getValue();
				 JSONObject jsonObject2 = new JSONObject();
				 jsonObject2.put("categoryName",list2.get(0).getCategory2Name());
				 jsonObject2.put("categoryId",list2.get(0).getCategory2Id());

//				 Map<Long, List<BaseCategoryView>> map3 = list2.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory3Id));
//				 Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator3 = map3.entrySet().iterator();

				 List<JSONObject> category3List = list2.stream().map(baseCategoryView -> {
					 JSONObject jsonObject3 = new JSONObject();
					 jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
					 jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
					 return jsonObject3;
				 }).toList();
				 jsonObject2.put("categoryChild",category3List);
//				 while(){
//					 JSONObject jsonObject3 = new JSONObject();
//					 Map.Entry<Long, List<BaseCategoryView>> next3 = iterator3.next();
//					 List<BaseCategoryView> list3 = next3.getValue();
//
//					 jsonObject3.put("categoryName",list3.get(0).getCategory2Name());
//					 jsonObject3.put("categoryId",next3.getKey());
//					 jsonObject2.put("categoryChild",jsonObject3);
//				 }

				 child2.add(jsonObject2);

			 }

			 jsonObject.put("categoryChild",child2);
			list.add(jsonObject);

		 }




		return list;
	}
}
